package com.damuzee.system.admin.action;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.mybatis.sql.page.Page;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import com.damuzee.system.admin.domain.AcctDetail;
import com.damuzee.system.admin.domain.AgentAcct;
import com.damuzee.system.admin.domain.CustInfo;
import com.damuzee.system.admin.domain.EmpUser;
import com.damuzee.system.admin.persistence.AcctDetailMapper;
import com.damuzee.system.admin.service.AgentAcctService;
import com.damuzee.system.admin.web.RequestUtil;

@Controller
public class AgentAcctAct {

	private static final Logger log = LoggerFactory .getLogger(AgentAcctAct.class);
	
	@RequestMapping("/agentacct/v_list.do")
	public String list(Integer pageNo,HttpServletRequest request, ModelMap model){
		
		EmpUser currUser  = RequestUtil.getUser();
		AgentAcct agentAcct = new AgentAcct();
		
		if (!currUser.isReadOnly()) {
			agentAcct.setCheckUser(1);
		}
		
		Page<AgentAcct> page = new Page<AgentAcct>();
		page.setCurrentPage(pageNo);
		
		 page = agentAcctService.getAgentAcctList(page, agentAcct);
		model.addAttribute("page", page.getPage());
		
		return "/agentacct/list" ;
	}
	
	@RequestMapping("/agentacct/v_add.do")
	public String add(){
		
		return "/agentacct/add" ;
	}
	
	
	@RequestMapping("/agentacct/o_save.do")
	public String save(String custName ,String address ,String telphone ,
			Float amount ,String dealStartDate ,String dealEndDate,
			String chargeStopDate ,String signStopDate ,HttpServletRequest request){
		CustInfo cust = new CustInfo();
		cust.setAddress(address);
		cust.setCustName(custName);
		cust.setTelphone(telphone);
		
		AgentAcct acct = new AgentAcct();
		acct.setAmount(amount);
		acct.setDealEndDate(dealEndDate);
		acct.setDealStartDate(dealStartDate);
		acct.setSignStopDate(signStopDate);
		acct.setChargeStopDate(chargeStopDate);
		
		EmpUser currUser  = RequestUtil.getUser();
		acct.setCheckUser(currUser.getId());
		
		agentAcctService.saveAgentAcct(cust,acct);
		return "redirect:v_list.do" ;
	}
	
	@RequestMapping("/agentacct/v_pay.do")
	public String viewPay(Integer acctId,ModelMap map){
		map.put("acctId", acctId);
		return "/agentacct/pay" ;
	}
	
	@RequestMapping("/agentacct/save_pay.do")
	public String savePay(AcctDetail detail,HttpServletRequest request){
		EmpUser user = RequestUtil.getUser();
		detail.setChargeUser(user.getId());
		
		//acctDetailMapper.save(detail);
		
		agentAcctService.saveAcctDetail(detail);
		return "redirect:v_list.do" ;
	}
	
	@RequestMapping("/agentacct/v_detail.do")
	public String detail(Integer acctId,Integer pageNo,HttpServletRequest request,ModelMap map){
	
		
		
		AcctDetail detail = new AcctDetail();
		detail.setAcctId(acctId);
		
		Page<AcctDetail> page = new Page<AcctDetail>();
		page = acctDetailMapper.selectAcctDetailByPage(page, detail);
		page.setCurrentPage(pageNo);
		
		map.addAttribute("acctId", acctId);
		map.addAttribute("page", page.getPage());
		
		return "/agentacct/detail" ;
	}
	
	@RequestMapping("/agentacct/v_left.do")
	public String left(){
		
		return "/agentacct/left" ;
	}
	
	
	@Autowired
	private AgentAcctService agentAcctService;
	@Resource
	private AcctDetailMapper acctDetailMapper ;
}
